/*

  Flat-frame correction

  Copyright © 2010 - 2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
// #include <iostream>
// #include <string>
// #include <cstring>
// #include <cstdio>
// #include <cerrno>
// #include <cstdlib>



bool Munipack::flat(MuniCmdLineParser& cmd)
{
  //  action = new MuniAction(this,"flat",true);
  MuniProcess *action = new MuniProcess(&pipe,"flat");
  pipe.push(action);

  wxString a, flag;

  if( cmd.Found("x",&a) )
    flag = a;

  if( cmd.Found("f",&a) )
    action << ("Flatframe = '" + a + "'");

  action->Write(cmd.GetParams(),flag,wxString("_F"));

  /*
  action->AddFilesInput(cmd,flag,"_F");
  action->CloseOutput();
  */

  pipe.Start();

  return true;
}


// using namespace std;


// int flat(int argc, const char *argv[])
// {
//   // init, help
//   int arg0 = argc;
//   for(int i = 1; i < argc; i++) {

//     if( strcmp(argv[i],"flat") == 0 ) 
//       arg0 = i + 1;

//     if( strcmp(argv[i],"--help") == 0 || arg0 == argc ) {
//       cout << "munipack flat [-x flag] [-b bitpix] flatfile file(s)[,result(s)]"
// 	   << endl;
//       return 0;
//     }
//   }


//   // open and load pipe
//   FILE *p;
//   //  if( (p = popen("cat","w")) == NULL ) {
//   if( (p = popen("flat","w")) == NULL ) {
//     cerr << strerror(errno) << endl;
//     return errno;
//   }

//   fitskeys(p);

//   bool flatname = false;
//   const char *flag = 0;
//   for(int i = arg0; i < argc; i++) {

//     if( strcmp(argv[i],"-x") == 0 && i++ < argc)
//       flag = argv[i];
    
//     else if( strcmp(argv[i],"-b") == 0 && i++ < argc ) 
//       fprintf(p,"Output BITPIX = %s\n",argv[i]);

//     else if( strcmp(argv[i],"-f") == 0 && i++ < argc ) {
//       fprintf(p,"Flatframe = '%s'\n",argv[i]);
//       flatname = true;
//     }

//     else if( strcmp(argv[i],"-") == 0 ) {
//       char line[32768];
//       while( true ) {
// 	cin.getline(line,32768);
// 	if( ! cin.good() ) break;
	
// 	string orig,result;
// 	parsename(flag,"_F",line,orig,result);
// 	fprintf(p,"'%s' '%s'\n",orig.c_str(),result.c_str());
//       }
//     }

//     else {

//       if( ! flatname ) {
// 	fprintf(p,"Flatframe = '%s'\n",argv[i]);
// 	flatname = true;
//       }
//       else {
// 	string line(argv[i]);
// 	string orig,result;
// 	parsename(flag,"_F",line,orig,result);
// 	fprintf(p,"'%s' '%s'\n",orig.c_str(),result.c_str());
//       }
//     }
//   }
//   pclose(p);

//   return errno;
// }
